home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
common
/
proxysockets.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
17KB
|
484 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
from util import GetProxyInfo
from AsyncSocket import AsyncSocket as asocket
import socks
from socket import _socket as socket
from functools import wraps
import sys
import logging
log = logging.getLogger('proxysockets')
class ProxyType:
SOCKS4 = socks.PROXY_TYPE_SOCKS4
SOCKS5 = socks.PROXY_TYPE_SOCKS5
HTTP = socks.PROXY_TYPE_HTTP
HTTPS = socks.PROXY_TYPE_HTTPS
class ProxySocket(asocket):
def __init__(self, proxy, conn, post, callback):
asocket.__init__(self, conn)
self.callback = callback
self.post_negotiate = post
self._negotiating = False
if proxy is None:
proxy = { }
self._proxyinfo = proxy.copy()
self.mid = (self._proxyinfo.get('addr', ''), self._proxyinfo.get('port', 0))
self.end = ('', 0)
def connect(self, end):
self.end = end
self._pconnect()
def _pconnect(self):
asocket.connect(self, self.mid)
def handle_close(self):
log.info('ProxySocket.handle_close - calling callback.error')
self.close()
asocket.handle_close(self)
def close(self):
if getattr(self, 'socket', None) is not None:
asocket.close(self)
if self.callback is not None:
self.callback = None
cb = self.callback
cb.error()
def handle_connect(self):
if not self._negotiating:
log.info('ProxySocket connected. starting negotiation... ')
self._negotiating = True
self._pnegotiate()
def _pnegotiate(self):
negs = {
ProxyType.HTTP: self._negotiatehttp,
ProxyType.HTTPS: self._negotiatehttps,
ProxyType.SOCKS4: self._negotiatesocks4,
ProxyType.SOCKS5: self._negotiatesocks5 }
neg = negs.get(self._proxyinfo.get('proxytype', None), self._negotiation_done)
neg()
def _negotiatehttp(self):
try:
endhost = self._endhost_resolved = socket.gethostbyname(self.end[0])
except socket.gaierror:
self._endhost_resolved = None
endhost = self.end[0]
endport = self.end[1]
authstr = self._httpauthstring()
http_connect = 'CONNECT %s:%d HTTP/1.1\r\nHost: %s\r\n%s\r\n' % (endhost, endport, self.end[0], authstr)
None(log.info, 'ProxySocket._negotiatehttp: sending proxy CONNECT%s. %r:%r' if authstr else '', self, (endhost, endport))
self.push(http_connect)
self.push_handler(self._httpfinish)
self.set_terminator('\r\n\r\n')
def _httpauthstring(self):
username = self._proxyinfo.get('username', None)
password = self._proxyinfo.get('password', None)
if all((username, password)):
raw = '%s:%s' % (username, password)
auth = 'Basic %s' % ''.join(raw.encode('base-64').strip().split())
return 'Proxy-Authorization: %s\r\n' % auth
else:
return ''
def _httpfinish(self, data):
self.pop_handler()
statusline = data.splitlines()[0].split(' ', 2)
if statusline[0] not in ('HTTP/1.0', 'HTTP/1.1'):
log.info('ProxySocket._httpfinish: Bad data from server, disconnecting: (%r)', data)
return self.close()
try:
statuscode = int(statusline[1])
except ValueError:
log.info('ProxySocket._httpfinish: Got some bad data from the server, disconnecting: %r (%r)', statusline, data)
return self.close()
if statuscode != 200:
log.info('ProxySocket._httpfinish: got HTTPError code %r, disconnecting (%r)', statuscode, data)
return self.close()
log.info('ProxySocket._httpfinish: success %r', self)
self._ProxySocket__proxysockname = ('0.0.0.0', 0)
if not self._endhost_resolved:
pass
self._ProxySocket__proxypeername = (self.end[0], self.end[1])
self._negotiation_done()
def _negotiation_done(self):
log.info('proxy negotiation complete')
self._proxy_setup = True
self.del_channel()
self.finish(self.socket, 'handle_connect')
if self.callback is not None:
self.callback.success()
self.callback = None
self.socket = None
def finish(self, sck, handler_name):
sck = self.post_negotiate(sck)
sck.connected = True
sck._proxy_setup = True
self.collect_incoming_data = sck.collect_incoming_data
getattr(sck, handler_name)()
def _negotiatehttps(self):
raise NotImplementedError
def _negotiatesocks4(self):
pack = pack
import struct
destaddr = self.end[0]
destport = self.end[1]
rresolve = False
def zstring(s):
return s + '\x00'
try:
ipaddr = socket.inet_aton(destaddr)
except socket.error:
try:
ipaddr = socket.inet_aton(socket.gethostbyname(destaddr))
except socket.error:
str
ipaddr = pack('!I', 1)
rresolve = True
except:
None<EXCEPTION MATCH>socket.error
None<EXCEPTION MATCH>socket.error
req = pack('!BBH', 4, 1, destport) + ipaddr
username = self._proxyinfo.get('username', '')
req += zstring(username)
if rresolve:
req += zstring(destaddr)
log.info('ProxySocket._negotiatesocks4: sending request')
self.push(req)
self.push_handler(self._socks4finish)
self.set_terminator(8)
if rresolve:
self._ProxySocket__proxypeername = (socket.inet_ntoa(ipaddr), destport)
else:
self._ProxySocket__proxypeername = (destaddr, destport)
def _socks4finish(self, data):
unpack = unpack
import struct
self.pop_handler()
log.info('ProxySocket._negotiatesocks4: received response')
try:
(null, returncode, port, ip) = unpack('!BBH4s', data)
except Exception:
e = None
log.info('ProxySocket._negotiatesocks4: bad data received from server. original exception is: %r', e)
return self.close()
ip = socket.inet_ntoa(ip)
if null != 0:
log.info('ProxySocket._negotiatesocks4: Bad data from server- expected null byte, got %r', null)
return self.close()
if returncode != 90:
log.info('ProxySocket._negotiatesocks4: received error code %r', returncode)
return self.close()
log.info('ProxySocket._negotiatesocks4: success')
self._ProxySocket__proxysockname = (ip, port)
self._negotiation_done()
def _negotiatesocks5_gen(self):
pack = pack
unpack = unpack
import struct
Storage = Storage
import util
(destaddr, destport) = self.end
uname = self._proxyinfo.get('username', '')
password = self._proxyinfo.get('password', '')
this = Storage()
this.errors = False
this.authtype = 0
this.incoming_host_type = 0
def pstring(s):
return chr(len(s)) + s
def single_use_handler(f):
def wrapper(data):
self.pop_handler()
return f(data)
wrapper = (None, wraps(f))(wrapper)
return wrapper
def if_errors_close(f):
def wrapper(*a, **k):
ok = not (this.errors)
if ok:
try:
return f(*a, **k)
except Exception:
e = None
log.info('ProxySocket._negotiatesocks5: there was an error calling %r(*%r, **%r). the exception was: %r', f, a, k, e)
this.errors = True
self.close()
return ('', None)
except:
None<EXCEPTION MATCH>Exception
None<EXCEPTION MATCH>Exception
log.info('ProxySocket._negotiatesocks5: Previous errors prevented %r(*%r, **%r) from happening', f, a, k)
return ('', None)
return wrapper
sender = if_errors_close
def recver(f):
return if_errors_close(single_use_handler(f))
def _sendauthtype():
if uname and password:
data = pack('!BBBB', 5, 2, 0, 2)
else:
data = pack('!BBB', 5, 1, 0)
return (data, 2)
_sendauthtype = (None, (None, (None, (None,))), sender)(_sendauthtype)
def _recvauthtype(data):
(status, authmethod) = unpack('!BB', data)
if status != 5:
raise Exception('Bad data was received from the proxy server: %r', data)
if authmethod in (0, 2):
this.authtype = authmethod
elif authmethod == 255:
this.authtype = None
raise Exception('All auth methods were rejected')
_recvauthtype = (None, recver)(_recvauthtype)
def _sendauth():
return (chr(1) + pstring(uname) + pstring(password), 2)
_sendauth = (None, None, sender)(_sendauth)
def _recvauth(data):
(code, status) = map(ord, data)
if code != 1:
raise Exception('Was expecting 1, got %r', code)
if status != 0:
raise Exception('authentication failed. bad uname/pword?')
_recvauth = recver(_recvauth)
def _sendproxysetup():
request = pack('!BBB', 5, 1, 0)
rresolve = False
try:
this.resolved_ip = socket.inet_aton(destaddr)
except socket.error:
try:
this.resolved_ip = socket.inet_aton(socket.gethostbyname(destaddr))
this.resolved_ip = None
rresolve = True
if rresolve:
request += chr(3) + pstring(destaddr)
else:
request += chr(1) + this.resolved_ip
request += pack('!H', destport)
return (request, 4)
_sendproxysetup = (None, None, None, None, sender)(_sendproxysetup)
def _recvproxysetup(data):
(five, null, _unused, status) = map(ord, data)
if five != 5:
raise Exception('Was expecting 5, got: %r', five)
if null != 0:
raise Exception('Connection failed, reason code was: %r', null)
if status in (1, 3):
this.incoming_host_type = status
return None
raise Exception('Unknown error occurred.')
_recvproxysetup = (recver,)(_recvproxysetup)
def _sendpstringhost1():
return ('', 1)
_sendpstringhost1 = sender(_sendpstringhost1)
def _recvpstringhost1(data):
this.hostlen = ord(data)
_recvpstringhost1 = (recver,)(_recvpstringhost1)
def _sendpstringhost2():
return ('', this.hostlen)
_sendpstringhost2 = (sender,)(_sendpstringhost2)
def _recvpstringhost2(data):
this.boundhost = data
_recvpstringhost2 = (recver,)(_recvpstringhost2)
def _sendiphost():
return ('', 4)
_sendiphost = sender(_sendiphost)
def _recvhost(data):
this.boundhost = socket.inet_ntoa(data)
_recvhost = (recver,)(_recvhost)
def _getport():
return ('', 2)
_getport = sender(_getport)
def _recvport(data):
(this.boundport,) = unpack('!H', data)
_recvport = (None, recver)(_recvport)
steps = ((_recvauthtype, _sendauthtype, None), (_recvauth, (_sendauth,), (lambda : this.authtype)), (_recvproxysetup, _sendproxysetup, None), (_recvhost, (_sendiphost,), (lambda : this.incoming_host_type == 1)), (_recvpstringhost1, (_sendpstringhost1,), (lambda : this.incoming_host_type == 3)), (_recvpstringhost2, (_sendpstringhost2,), (lambda : this.incoming_host_type == 3)), (_recvport, _getport, None))
for recvr, sendr, check in steps:
if check is None or check():
yield sendr
None(recvr)
continue
self._ProxySocket__proxysockname = (this.boundhost, this.boundport)
if this.resolved_ip is not None:
self._ProxySocket__proxypeername = (socket.inet_ntoa(this.resolved_ip), destport)
else:
self._ProxySocket__proxypeername = (destaddr, destport)
if not this.errors:
self._negotiation_done()
else:
self.close()
def _negotiatesocks5(self):
gen = self._negotiatesocks5_gen()
def handler(data):
log.info('ProxySocket._negotiatesocks5: in =%r', data)
try:
next = gen.send(data)
except StopIteration:
return None
(data, term) = next()
if data and ord(data[0]) == 1:
logdata = '<authstring omitted>'
else:
logdata = data
log.info('ProxySocket._negotiatesocks5: out=%r, terminator=%r', logdata, term)
self.push(data)
self.push_handler(handler)
self.set_terminator(term)
handler(None)
def __repr__(self):
parentrepr = asocket.__repr__(self).strip('<>')
return '<%s, fileno=%r>' % (parentrepr, self._fileno)
def main():
testapp = testapp
import tests.testapp
end = end
import AsyncoreThread
import wx as wx
a = testapp('../..')
a.toggle_crust()
class HeadSocket(asocket):
def __init__(self, *a, **k):
asocket.__init__(self, *a, **k)
self.set_terminator('\r\n\r\n')
self.push('HEAD / HTTP/1.0\r\n\r\n')
self.push_handler((lambda d: log.info(repr(d))))
def GetProxyInfo(self):
return { }
h = HeadSocket()
h.connect(('www.google.com', 80), success = (lambda s = h: log.info('success! socket: %r', s)))
a.MainLoop()
end()
if __name__ == '__main__':
def GetProxyInfo():
return dict(proxytype = 3, username = 'digsby', password = 'password', addr = 'athena', port = 9999)
print main()